import jwt


class MyJwt:
    def __init__(self):
        self.secret = 'awtrsgsdgsdgasdger'

    # 加密
    def jwt_encode(self, payload):
        return jwt.encode(payload, self.secret, algorithm='HS256')

    # 解密
    def jwt_decode(self, token):
        # payload = jwt.decode(token, self.secret, algorithms='HS256', options={"verify_signature": False})
        payload = jwt.decode(token, self.secret, algorithms='HS256')
        return payload

    # 对比
    def check(self, token):
        payload = jwt.decode(token, self.secret, algorithms='HS256')
        newtoken = jwt.encode(payload, self.secret, algorithm='HS256')

        if token == newtoken:
            # 验证过期时间
            nowtime = int(time.time())
            if nowtime - payload['user']['exp'] > 3600000:
                return False
            return True
        return False


myjwt = MyJwt()
import time
# newtime = int(time.time())
# data = {'user': {'name': 'zhangsan', 'exp': newtime}}
# token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7Im5hbWUiOiJ6aGFuZ3NhbiIsImV4cCI6MTY2MDYzNzMxNX19.gug8FpWuiupdWlyJ-cAAs5bkJN9jqIinY5m96CoeNT4'
# payload = myjwt.jwt_encode(data)
# payload = myjwt.jwt_decode(token)
# payload = myjwt.check(token)
# print(payload)
